package org.thanos.iot.service.graphic.impl;

import com.honeywell.tj.iot.entity.graphic.GraphicCategory;
import com.honeywell.tj.iot.enums.graphic.CategoryType;
import com.honeywell.tj.iot.mapper.graphic.GraphicCategoryMapper;
import com.honeywell.tj.iot.mapper.graphic.GraphicComponentFileMapper;
import com.honeywell.tj.iot.service.graphic.GraphicCategoryService;
import com.honeywell.tj.iot.vo.graphic.ComponentThumbnailVo;
import com.honeywell.tj.iot.vo.graphic.GraphicCategoryVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service("graphicCategoryService")
public class GraphicCategoryServiceImpl implements com.honeywell.tj.iot.service.graphic.GraphicCategoryService {

    @Resource
    private GraphicCategoryMapper categoryMapper;

    @Resource
    private GraphicComponentFileMapper fileMapper;

    @Override
    public List<ComponentThumbnailVo> getComponentThumbnail(Integer componentId) {
        return fileMapper.selectComponentLogicAndPathByComponentId(componentId);
    }

    @Override
    public List<GraphicCategory> getCategoriesByParentIds(List<Integer> parentIds) {
        return categoryMapper.selectCategoriesByParentIds(parentIds);
    }

    @Override
    public List<GraphicCategoryVo> getCategoriesSelective() {
        List<GraphicCategory> graphicCategories = categoryMapper.selectCategoriesSelective();
        if (CollectionUtils.isEmpty(graphicCategories)) {
            return Collections.emptyList();
        }
        return graphicCategories.stream().map(graphicCategory -> {
            GraphicCategoryVo graphicCategoryVo = new GraphicCategoryVo();
            graphicCategoryVo.setId(graphicCategory.getId());
            graphicCategoryVo.setName(graphicCategory.getName());
            graphicCategoryVo.setParentId(graphicCategory.getParentId());
            return graphicCategoryVo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<GraphicCategory> getCategories() {
        return categoryMapper.selectCategories(CategoryType.CUSTOM.getType());
    }

    @Override
    public Integer addCategory(String name, Integer parentId) {
        GraphicCategory graphicCategory = new GraphicCategory();
        graphicCategory.setName(name);
        graphicCategory.setParentId(parentId);
        graphicCategory.setIsCustomized(CategoryType.CUSTOM.getType());
        categoryMapper.insertCategory(graphicCategory);
        return graphicCategory.getId();
    }

    @Override
    public Integer updateCategory(String name, Integer id) {
        return categoryMapper.updateCategory(id, name);
    }

    @Override
    public int deleteCategories(List<Integer> ids) {
        return categoryMapper.deleteCategoriesByIds(ids);
    }
}
